package com.example.yam.greenapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 杨建利 on 2017/2/4
 * 评论适配器
 */

public class PinglunsAdapter extends ArrayAdapter<Pinglun> {
    private int resourceId;
    public PinglunsAdapter(Context context, int textViewResourceId, List objects) {
        super(context,textViewResourceId, objects);
        this.resourceId=textViewResourceId;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Pinglun pinglun=getItem(position);
        View view;
        ViewHoder viewHoder;
        if(convertView == null) {//如果没有进行过布局

            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            viewHoder = new ViewHoder();

            viewHoder.pinglun_content = (TextView) view.findViewById(R.id.pinglun_content);
            viewHoder.pinglun_score = (TextView) view.findViewById(R.id.pinglun_star);
            viewHoder.pinglun_uname = (TextView) view.findViewById(R.id.pinglun_uname);
            viewHoder.pinglun_pdate = (TextView) view.findViewById(R.id.pinglun_pdate);
            //将viewHoder存储在view的Tag中
            view.setTag(viewHoder);
        }else {
            view = convertView;
            viewHoder = (ViewHoder) view.getTag();//重新获取viewhoder
        }
        viewHoder.pinglun_content.setText(pinglun.getContent());
        viewHoder.pinglun_score.setText("评分:"+pinglun.getStar());
        viewHoder.pinglun_uname.setText(pinglun.getUname());
        viewHoder.pinglun_pdate.setText(pinglun.getPdate());

        return view;
    }
    class ViewHoder{//缓存控件
        TextView pinglun_content;//内容
        TextView pinglun_score;//分数
        TextView pinglun_uname;//评论者名字
        TextView pinglun_pdate;//评论时间
    }

}
